package com.fzshang.shopping.api.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fzshang.shopping.commen.bean.Classification;
import com.fzshang.shopping.commen.bean.Money;
import com.fzshang.shopping.commen.bean.layuiBean.ResultPage;
import com.fzshang.shopping.commen.bean.layuiBean.VoPage;
import com.fzshang.shopping.commen.controller.BaseController;
import com.fzshang.shopping.commen.entity.ApiResult;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>
 * moeny 前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2020-03-11
 */
@Controller
@RequestMapping("admin/money")
public class MoneyController extends BaseController {

    @RequestMapping(value = "money.html")
    public String money(){
        return "admin/money/money";
    }
    @RequestMapping(value = "money_add.html")
    public String money_add(){
        return "admin/money/money_add";
    }
    @RequestMapping(value = "money_edit.html")
    public String money_edit(Model model, Long id){
        model.addAttribute("money",moneyService.getById(id));
        return "admin/money/money_edit";
    }

    /**
     * @param voPage
     * @return 后台查询
     */
    @RequestMapping(value = "selectMyPage",method = RequestMethod.GET)
    @ResponseBody
    public ResultPage<Money> selectMyPage(VoPage voPage ){
        return moneyService.selectMyPage(voPage);
    }

    /**
     * @param money
     * @return 添加和修改
     */
    @RequestMapping(value = "saveOrUpdate")
    @ResponseBody
    public ApiResult saveOrUpdate(@RequestBody Money money){
        if(money.getId()==null){
            if(money.getState()==1){
                QueryWrapper queryWrapper = new QueryWrapper();
                queryWrapper.eq("state",1);
                Money money1 = moneyService.getOne(queryWrapper);
                if(money1!=null){
                    return ApiResult.fail("已经存在商家Money!");
                }
            }
            if(money.getState()==2){
                QueryWrapper queryWrapper1 = new QueryWrapper();
                queryWrapper1.eq("state",2);
                Money money2 = moneyService.getOne(queryWrapper1);
                if(money2!=null){
                    return ApiResult.fail("已经存在销售Money!");
                }
            }
        }

        money.setCreateTime(System.currentTimeMillis());
        if(moneyService.saveOrUpdate(money)){
            return ApiResult.ok();
        }
        return ApiResult.fail();
    }


    /**
     * @param id
     * @return 删除
     */
    @RequestMapping(value = "deleteById")
    @ResponseBody
    public ApiResult deleteById(Long id){
        if(moneyService.removeById(id)){
            return ApiResult.ok();
        }
        return ApiResult.fail();
    }


}
